#pragma once
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string>
#include <sys/types.h>
#include <string.h>
#include "./Log/sqylog.hpp"
using namespace std;

const string pathname = "/home/sqy";
const int proj_id = 0x6666;
const int size = 4096;
key_t GetKey()
{
    key_t key= ftok(pathname.c_str(),proj_id);
    if(key < 0)
    {
        FATAL("ftok e6rror: %s",strerror(errno));
        exit(1);
    }
    INFO("ftok success, key is %d",key);
    return key;;
}
int GetShareMemHelper(int flag)
{
    key_t k = GetKey();
    int shmid = shmget(k,size,flag);
    if(shmid < 0)
    {
        FATAL("create share memory error: %s",strerror(errno));
        exit(2);
    }
    INFO("create share success,shmid :%d", shmid);
    return shmid;
}

int CreateShm()
{
    return GetShareMemHelper(IPC_CREAT | IPC_EXCL | 0666);
}

int GetShm()
{
    return GetShareMemHelper(IPC_CREAT);
}